<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveModel::Serialization</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveModel::Serialization 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activemodel/lib/active_model/serialization_rb.html">activemodel/lib/active_model/serialization.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<h2 id="label-Active+Model+Serialization">Active Model Serialization</h2>

<p>Provides a basic serialization to a <a
href="Serialization.html#method-i-serializable_hash">#serializable_hash</a>
for your object.</p>

<p>A minimal implementation could be:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Serialization</span>

  <span class="ruby-identifier">attr_accessor</span> :<span class="ruby-identifier">name</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">attributes</span>
    {<span class="ruby-string">'name'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>}
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Which would provide you with:</p>

<pre class="ruby"><span class="ruby-identifier">person</span> = <span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">serializable_hash</span>   <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;nil}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">&quot;Bob&quot;</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">serializable_hash</span>   <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;Bob&quot;}</span>
</pre>

<p>You need to declare an attributes hash which contains the attributes you
want to serialize. Attributes must be strings, not symbols. When called,
serializable hash will use instance methods that match the name of the
attributes hash’s keys. In order to override this behavior, take a look at
the private method <code>read_attribute_for_serialization</code>.</p>

<p>Most of the time though, you will want to include the JSON or XML
serializations. Both of these modules automatically include the
<code>ActiveModel::Serialization</code> module, so there is no need to
explicitly include it.</p>

<p>A minimal implementation including XML and JSON would be:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Serializers</span><span class="ruby-operator">::</span><span class="ruby-constant">JSON</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Serializers</span><span class="ruby-operator">::</span><span class="ruby-constant">Xml</span>

  <span class="ruby-identifier">attr_accessor</span> :<span class="ruby-identifier">name</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">attributes</span>
    {<span class="ruby-string">'name'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>}
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Which would provide you with:</p>

<pre class="ruby"><span class="ruby-identifier">person</span> = <span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">serializable_hash</span>   <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;nil}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">as_json</span>             <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;nil}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">to_json</span>             <span class="ruby-comment"># =&gt; &quot;{\&quot;name\&quot;:null}&quot;</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">to_xml</span>              <span class="ruby-comment"># =&gt; &quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;\n&lt;serial-person...</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">&quot;Bob&quot;</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">serializable_hash</span>   <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;Bob&quot;}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">as_json</span>             <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;Bob&quot;}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">to_json</span>             <span class="ruby-comment"># =&gt; &quot;{\&quot;name\&quot;:\&quot;Bob\&quot;}&quot;</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">to_xml</span>              <span class="ruby-comment"># =&gt; &quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;\n&lt;serial-person...</span>
</pre>

<p>Valid options are <code>:only</code>, <code>:except</code>,
<code>:methods</code> and <code>:include</code>. The following are all
valid examples:</p>

<pre>person.serializable_hash(only: 'name')
person.serializable_hash(include: :address)
person.serializable_hash(include: { address: { only: 'city' }})</pre>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Serialization.html#method-i-serializable_hash">serializable_hash</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-serializable_hash">
            
              <b>serializable_hash</b>(options = nil)
            
            <a href="Serialization.html#method-i-serializable_hash" name="method-i-serializable_hash" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a serialized hash of your object.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Serialization</span>

  <span class="ruby-identifier">attr_accessor</span> :<span class="ruby-identifier">name</span>, :<span class="ruby-identifier">age</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">attributes</span>
    {<span class="ruby-string">'name'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>, <span class="ruby-string">'age'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>}
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">capitalized_name</span>
    <span class="ruby-identifier">name</span>.<span class="ruby-identifier">capitalize</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span> = <span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'bob'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">age</span>  = <span class="ruby-value">22</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">serializable_hash</span>                <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;bob&quot;, &quot;age&quot;=&gt;22}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">serializable_hash</span>(<span class="ruby-identifier">only</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">name</span>)   <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;bob&quot;}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">serializable_hash</span>(<span class="ruby-identifier">except</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">name</span>) <span class="ruby-comment"># =&gt; {&quot;age&quot;=&gt;22}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">serializable_hash</span>(<span class="ruby-identifier">methods</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">capitalized_name</span>)
<span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;bob&quot;, &quot;age&quot;=&gt;22, &quot;capitalized_name&quot;=&gt;&quot;Bob&quot;}</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-serializable_hash_source')" id="l_method-i-serializable_hash_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/019df9887519701b321c668c1d27d5c0c26fee7a/activemodel/lib/active_model/serialization.rb#L97" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-serializable_hash_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/serialization.rb, line 97</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">serializable_hash</span>(<span class="ruby-identifier">options</span> = <span class="ruby-keyword">nil</span>)
  <span class="ruby-identifier">options</span> <span class="ruby-operator">||=</span> {}

  <span class="ruby-identifier">attribute_names</span> = <span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">keys</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">only</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:only</span>]
    <span class="ruby-identifier">attribute_names</span> <span class="ruby-operator">&amp;=</span> <span class="ruby-constant">Array</span>(<span class="ruby-identifier">only</span>).<span class="ruby-identifier">map</span>(&amp;<span class="ruby-value">:to_s</span>)
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">except</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:except</span>]
    <span class="ruby-identifier">attribute_names</span> <span class="ruby-operator">-=</span> <span class="ruby-constant">Array</span>(<span class="ruby-identifier">except</span>).<span class="ruby-identifier">map</span>(&amp;<span class="ruby-value">:to_s</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">hash</span> = {}
  <span class="ruby-identifier">attribute_names</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-identifier">hash</span>[<span class="ruby-identifier">n</span>] = <span class="ruby-identifier">read_attribute_for_serialization</span>(<span class="ruby-identifier">n</span>) }

  <span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:methods</span>]).<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">m</span><span class="ruby-operator">|</span> <span class="ruby-identifier">hash</span>[<span class="ruby-identifier">m</span>.<span class="ruby-identifier">to_s</span>] = <span class="ruby-identifier">send</span>(<span class="ruby-identifier">m</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">m</span>) }

  <span class="ruby-identifier">serializable_add_includes</span>(<span class="ruby-identifier">options</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">association</span>, <span class="ruby-identifier">records</span>, <span class="ruby-identifier">opts</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">hash</span>[<span class="ruby-identifier">association</span>.<span class="ruby-identifier">to_s</span>] = <span class="ruby-keyword">if</span> <span class="ruby-identifier">records</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:to_ary</span>)
      <span class="ruby-identifier">records</span>.<span class="ruby-identifier">to_ary</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">serializable_hash</span>(<span class="ruby-identifier">opts</span>) }
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">records</span>.<span class="ruby-identifier">serializable_hash</span>(<span class="ruby-identifier">opts</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">hash</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    